<template>
	<!-- 下达修改 -->
	<view class="p-10">
		<view class="card">
			<my-forms ref="valiForm" :model="valiFormData" :label-position="'top'" :formItem="formData" label-width="120px">
				<template #signFileId>
					<my-signature @getId="getId" @getUrl="getUrl" :url="valiFormData.signFileUrl"></my-signature>
				</template>
			</my-forms>
			<u-button type="primary" shape="circle" text="提交" @click="submit"></u-button>
		</view>
	</view>
</template>
<script setup>
import { computed, reactive, ref, unref } from 'vue';
import { onLoad } from '@dcloudio/uni-app';
import date from '@/utils/date.js';
import { acceptPostpone, getAcceptSign } from '@/api/hidden-danger.js';
import { apiBaseURL } from '@/config/index.js';

// 校验表单数据
const formData = computed(() => {
	return [
		{
			required: true,
			itemType: 'radio',
			label: '验收',
			list: [
				{
					label: '通过'
				},
				{
					label: '驳回'
				}
			],
			prop: 'state'
		},
		{
			itemType: 'date',
			label: '验收日期',
			required: true,
			prop: 'accetpTime'
		},
		{
			itemType: 'cascader',
			type: 'GETdeptTree',
			label: '验收单位',
			required: true,
			prop: 'accetpDeptId'
		},
		{
			itemType: 'select',
			type: 'GETuseList',
			multiple: true,
			label: '验收人',
			required: true,
			prop: 'checkUserIds'
		},
		{
			type: 'textarea',
			label: '验收情况',
			required: true,
			prop: 'accetpMessage'
		},
		valiFormData.state == 1 && {
			itemType: 'date',
			type: 'date',
			required: true,
			label: '限制二次整改日期',
			prop: 'nextChangeDate'
		},
		// {
		// 	label: '签名',
		// 	prop: 'signFileId',
		// },
		{
			itemType: 'image',
			label: '上传验收图片',
			prop: 'filePaths'
		}
	].filter((i) => i);
});
const valiForm = ref(null);
const valiFormData = reactive({
	state: 0,
	accetpTime: date(new Date())
});
// 保存提交
const submit = () => {
	valiForm.value.$refs.refMyForm
		.validate()
		.then(async (res) => {
			console.log('success', res);
			const { code } = await acceptPostpone({
				checkPlanIds: [unref(id)],
				...unref(valiFormData)
			});
			if (code == 200) {
				uni.showToast({
					title: `验收通过`,
					success() {
						uni.navigateBack({
							delta: 2
						});
					}
				});
			}
			uni.showToast({
				title: `验收通过`
			});
		})
		.catch((err) => {
			console.log('err', err);
		});
};
let id = ref();
onLoad((e) => {
	id.value = e.id;
	getAcceptSign().then((res) => {
		if (res.data?.length) {
			valiFormData.signFileId = res.data[0]?.id;
			valiFormData.signFileUrl = apiBaseURL + '/rdfile/download/' + res.data[0]?.storageUrl;
		}
	});
});

const getId = (val) => {
	valiFormData.signFileId = val;
};
const getUrl = (val) => {
	valiFormData.signFileUrl = val;
};
</script>
<style lang="scss" scoped></style>
